package com.zmn.plat.business.impl.orderconfig.distribute;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.manager.redis.RedisManager;
import com.zmn.plat.business.interfaces.orderconfig.distribute.RobOrderConfigBService;
import com.zmn.plat.common.dto.orderconfig.distribute.RobOrderDIO;
import com.zmn.plat.model.entity.orderconfig.distribute.*;
import com.zmn.plat.services.interfaces.orderconfig.distribute.RobOrderConfigService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述:
 *
 * @param
 * @author tanlei
 * @return
 * @since 2021/7/12 11:11
 */
@Slf4j
@Service
public class RobOrderConfigBServiceImpl implements RobOrderConfigBService {

    @Resource
    protected RobOrderConfigService robOrderConfigService;

    @Resource
    private RedisManager redisManager;

    private static final String ROB_ORDER_CONFIG_DATA = "plat:orderConfig:robOrder";

    private static final String SNED_ORDER_CONFIG_DATA = "plat:orderConfig:sendOrder";


    private static final String ROB_ORDER = "robOrder";

    private static final String SEND_ORDER = "sendOrder";

    private static final int SECOND_OF_HOURS = 1800;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insert(RobOrderConfigInfo robOrderConfigInfo) {
        return robOrderConfigService.insert(robOrderConfigInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertBatch(List<RobOrderRelation> robOrderRelationList) {
        if (robOrderRelationList == null || robOrderRelationList.size() == 0) {
            return null;
        }
        return robOrderConfigService.insertBatch(robOrderRelationList);
    }

    @Override
    @Transactional
    public Integer insertBatchProduct(List<RobOrderRelationProduct> robOrderRelationProductList) {
        if(CollectionUtil.isNullOrEmpty(robOrderRelationProductList)){
            return null;
        }
        return robOrderConfigService.insertBatchProduct(robOrderRelationProductList);
    }

    @Override
    public Integer insertRelationProductData(RobOrderRelationProduct robOrderRelationProduct) {
        return robOrderConfigService.insertRelationProductData(robOrderRelationProduct);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertRelationData(RobOrderRelation robOrderRelation) {
        return robOrderConfigService.insertRelationData(robOrderRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateByKey(RobOrderConfigInfo robOrderConfigInfo) {
        return robOrderConfigService.updateByKey(robOrderConfigInfo);
    }

    @Override
    public Integer updateStatus(UpdateStatusQuery updateStatusQuery) {
        return robOrderConfigService.updateStatus(updateStatusQuery);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateRelationDataByKey(RobOrderRelation robOrderRelation) {
        return robOrderConfigService.updateRelationDataByKey(robOrderRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateBatchProductByKey(List<RobOrderRelationProduct> robOrderRelationProductList) {
        return robOrderConfigService.updateBatchProductByKey(robOrderRelationProductList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateBatchByKey(List<RobOrderRelation> robOrderRelationList) {
       if(CollectionUtil.isNullOrEmpty(robOrderRelationList)){
           return null;
       }
       return robOrderConfigService.updateBatchByKey(robOrderRelationList);
    }

    @Override
    public Integer countByQuery(RobOrderConfigInfoQuery query) {
        return robOrderConfigService.countByQuery(query);
    }

    @Override
    public List<RobOrderRelation> listByQuery(RobOrderRelationQuery query) {
        return robOrderConfigService.listByQuery(query);
    }

    @Override
    public List<RobOrderRelationProduct> listProductByQuery(RobOrderRelationProductQuery query) {
        return robOrderConfigService.listProductByQuery(query);
    }

    @Override
    public RobOrderConfigInfo findByKey(Integer configId) {
        if (NumberUtil.isNullOrZero(configId)) {
            return null;
        }
        return robOrderConfigService.findByKey(configId);

    }

    @Override
    public void deleteByConfigId(Integer configId) {
        robOrderConfigService.deleteByConfigId(configId);
    }

    @Override
    public void deleteByRelationId(Integer relationId) {
        robOrderConfigService.deleteByRelationId(relationId);
    }

    @Override
    public RobOrderConfigInfo findRobOrderConfigByQuery(RobOrderDIO query) {
        return robOrderConfigService.findRobOrderConfigByQuery(query);
    }

    @Override
    public RobOrderConfigInfo findCompanyRobOrderConfigByQuery(RobOrderDIO query) {
        return robOrderConfigService.findCompanyRobOrderConfigByQuery(query);
    }

    @Override
    public void setDataToRedis(String flag,Integer cityId, String data) {
        String redisKey = this.getRedisKey(flag,cityId);
        redisManager.setex(redisKey, data, SECOND_OF_HOURS);
    }

    @Override
    public String getDataFromRedis(String flag,Integer cityId) {
        String redisKey = this.getRedisKey(flag,cityId);
        return redisManager.get(redisKey);
    }

    @Override
    public void delDataFromRedis(String flag, Integer data) {
        String redisKey = this.getRedisKey(flag,data);
        redisManager.del(redisKey);
    }

    @NotNull
    @Contract(pure = true)
    private String getRedisKey(String flag,Integer cityId) {
        String str="";
        if(SEND_ORDER.equals(flag)){
            str = SNED_ORDER_CONFIG_DATA+ ":" + cityId;
        }
        if(ROB_ORDER.equals(flag)){
            str = ROB_ORDER_CONFIG_DATA + ":" + cityId;
        }
        return str;
    }
}
